//
//  FeedbackViewController.m
//  AILove
//
//  Created by 甘世清 on 2023/12/25.
//

#import "FeedbackViewController.h"
#import "feedbacktowViewController.h"
@interface FeedbackViewController ()<TZImagePickerControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic , strong) UIImage * image;
@property (nonatomic, strong) UIImagePickerController *imagePickerVc;
@property (nonatomic , strong) FSTextView * fsTextView;
@property (nonatomic , strong) UILabel * number;
@property (nonatomic , strong) SQCustomButton * addImage;
@property (nonatomic , strong) SQCustomButton * deleteBtn;
@property (nonatomic , strong) HeaderView * addButton;
@end

@implementation FeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"问题反馈");
    
}
- (void)SQ_UIViewForSelfView{
    UIView * bgView = [[UIView alloc]init];
    bgView .backgroundColor = SQHexColor(@"#303030", 1);
    bgView.layer.masksToBounds = YES;
    bgView.layer.cornerRadius = 20;
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDETH-30);
        make.top.equalTo(self.zx_navBar.mas_bottom).offset(20);
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(200);
    }];
    [bgView addSubview:self.fsTextView];
    [self.fsTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(bgView).insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
    self.number = [[UILabel alloc]init];
    self.number.textColor =  SQColor999999;
    self.number.font = FONT_MEDIUM(14);
    [self.view addSubview:self.number];
    [self.number mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgView.mas_right).offset(-10);
        make.bottom.equalTo(bgView.mas_bottom).offset(-10);
    }];
    self.number.text = @"(0/200)";
    WS(weakSelf);
    [self.fsTextView addTextDidChangeHandler:^(FSTextView *textView) {
        weakSelf.number.text = [NSString stringWithFormat:@"%lu/200",(unsigned long)textView.text.length];
        weakSelf.number.keywords = [NSString stringWithFormat:@"%lu/",(unsigned long)textView.text.length];;
        weakSelf.number.keywordsColor = UIColor.whiteColor;
        [weakSelf.number getLableSizeWithMaxWidth:200];
    }];
    
    
    [self.view addSubview:self.addImage];
    [self.addImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(115);
        make.height.mas_equalTo(130);
        make.left.equalTo(self.view).offset(6);
        make.top.equalTo(bgView.mas_bottom).offset(14);
    }];
  
    [self.addImage setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_headerButtonClick];
    }];
    
    [self.view addSubview:self.deleteBtn];
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(30);
        make.right.equalTo(self.addImage.mas_right);
        make.top.equalTo(bgView.mas_bottom).offset(12);
    }];
    [self.deleteBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_deleteButton];
    }];
    
    [self.view addSubview:self.addButton];
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDETH-60);
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight-30);
        make.height.mas_equalTo(48);
    }];
}
-(void)SQ_deleteButton{
    self.image = nil;
    self.addImage.image = SQGetImage(@"添加图片");
    self.deleteBtn.hidden = YES;
}
-(void)SQ_ButtonClick{
    
    if (self.fsTextView.formatText == nil) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请描述您的问题")];
        return;
    }
    if (self.image ==nil) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请添上传一张相关图片")];
        return;
    }
    WS(weakSelf);
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    [HttpTool SQ_Image:self.image successBlock:^(BaseModel * _Nonnull httpModel) {
        [weakSelf SQ_setUpName:httpModel.data];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
    
}
-(void)SQ_setUpName:(NSString*)header{
    if (![header isNotEmpty]) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"头像上传错误")];
        return;
    }
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:header forKey:@"img"];
    [dic setObject:self.fsTextView.formatText forKey:@"content"];
    [HttpTool post:Url19 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        feedbacktowViewController * vc = [[feedbacktowViewController alloc]init];
        [self SQ_pushViewControllerFull:vc];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
    
}

-(void)SQ_headerButtonClick{
    WS(weakSelf);
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:SQNSLocalString(@"上传照片") message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * co0 = [UIAlertAction actionWithTitle:SQNSLocalString(@"拍照") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf SQ_leftButtonClick];
    }];
    UIAlertAction * co1 = [UIAlertAction actionWithTitle:SQNSLocalString(@"相册") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf SQ_rightButtonClick];
    }];
    UIAlertAction * co2 = [UIAlertAction actionWithTitle:SQNSLocalString(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:co0];
    [alert addAction:co1];
    [alert addAction:co2];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)SQ_leftButtonClick{
    WS(weakSelf);
    _imagePickerVc = [[UIImagePickerController alloc] init];
    _imagePickerVc.delegate = self;
    _imagePickerVc.allowsEditing = YES;
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: sourceType]) {
        weakSelf.imagePickerVc.sourceType = sourceType;
        [self presentViewController:weakSelf.imagePickerVc animated:YES completion:nil];
    } else {
        NSLog(@"模拟器中无法打开照相机,请在真机中使用");
    }
}
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    WS(weakSelf);
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {
        UIImage *photo = [info objectForKey:UIImagePickerControllerEditedImage];
        if (photo == nil)
            {
                photo = [info objectForKey:UIImagePickerControllerOriginalImage];
            }
        [weakSelf SQ_caijian:photo];
    }else
    {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"相册数据异常")];
    }
}


-(void)SQ_rightButtonClick{
    WS(weakSelf);
    TZImagePickerController * vc = [[TZImagePickerController alloc]init];
    vc.maxImagesCount = 1;
    [self presentViewController:vc animated:YES completion:nil];
    [vc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        [weakSelf SQ_caijian:photos.firstObject ];
    }];
}

-(void)SQ_caijian:(UIImage*)image {
    self.addImage.image = image;
    self.image = image;
    self.deleteBtn.hidden = NO;
}


-(FSTextView*)fsTextView{
    if (!_fsTextView) {
        _fsTextView = [FSTextView textView];
        _fsTextView.font = FONT_MEDIUM(15);
        _fsTextView.textColor = UIColor.whiteColor;
        _fsTextView.placeholder =SQNSLocalString(@"请在这里描述您的问题～");
        _fsTextView.placeholderColor = SQColor999999;
        _fsTextView.backgroundColor = UIColor.clearColor;
        _fsTextView.maxLength = 200;
       
    }
    return _fsTextView;
}
- (SQCustomButton *)addImage{
    if (!_addImage) {
        _addImage = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(100, 100) midmargin:8];
        _addImage.text = SQNSLocalString(@"上传一张相关图片");
        _addImage.imageView.contentMode = UIViewContentModeScaleAspectFill;
        _addImage.imageView.clipsToBounds = YES;
        _addImage.textColor = SQColor(@"#999999");
        _addImage.font = FONT_MEDIUM(14);
        _addImage.image = SQGetImage(@"添加图片");
        _addImage.imageView.backgroundColor = SQColor(@"#303030");
        _addImage.imageView.layer.cornerRadius = 16;
    }
    return _addImage;
}
- (SQCustomButton *)deleteBtn{
    if (!_deleteBtn) {
        _deleteBtn = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(20, 20) midmargin:0];
        _deleteBtn.image = SQGetImage(@"删除");
        _deleteBtn.hidden = YES;
    }
    return _deleteBtn;
}
- (HeaderView *)addButton{
    if (!_addButton) {
        _addButton = [[HeaderView alloc]init];
        _addButton.image = @"矩形";
        _addButton.colorString = @"#FFFFFF";
        [_addButton.btn addTarget:self action:@selector(SQ_ButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _addButton.layer.cornerRadius = 6;
        _addButton.layer.masksToBounds = YES;
        _addButton.text = SQNSLocalString(@"提交");
        _addButton.font = FONT_MEDIUM(18);
    }
    return _addButton;
}
@end
